const { Log } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { loglevel, logmessage } = req.body
    try {
        switch (operation) {
            case 'create':
                const newLogData = await Log.create({
                    LogMessage: logmessage,
                    LogLevel: loglevel,
                    LogDate: new Date()
                })
                res.status(200).send({ message: '创建成功', data: newLogData })
                break;
            case 'read':
                const logData = await Log.findAll()
                console.log(logData);
                res.status(200).send({
                    data: logData,
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'delete':
                const { logID } = req.body
                const delData = await Log.destroy({ where: { LogId: logID } })
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            default:
                res.status(400).send({ message: 'Invalid operation' });
        }
    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}